//warning : only use cin and cout
#include <bits/stdc++.h>
#define F(i,l,r) for(int i=(l);i<=(r);i++)
#define DF(i,l,r) for(int i=(l);i>=(r);i--)
#define ms(x,y) memset(x,y,sizeof(x))
#define SZ(x) (int)x.size()-1
#define pb push_back
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int,int> pii;
template<typename T> void chkmin(T &x,T y){ x=min(x,y);}
template<typename T> void chkmax(T &x,T y){ x=max(x,y);}
const int N=150000;
int n,m,a[N],ori[N],c[N],dep;
string rd[20];
int X[4],lef[N<<1];
int sure[N<<1];
void build(int l,int r,int x,int curd){
    sure[x]=-1;
    if(l==r){ lef[x]=l;return;}
    int mid=(l+r)>>1;
    build(l,mid,x<<1,curd+1),build(mid+1,r,x<<1^1,curd+1);
    if(rd[curd][x-(1<<curd)]=='0'){
        if(a[lef[x<<1]]>=dep-curd) lef[x]=lef[x<<1];
        else lef[x]=lef[x<<1^1];
    }
    else{
        if(a[lef[x<<1^1]]>=dep-curd) lef[x]=lef[x<<1^1];
        else lef[x]=lef[x<<1];
    }
}
LL sum(int l,int r){ l++,r++;return 1ll*(l+r)*(r-l+1)/2;}
int use[N<<1],len;
void dfs1(int l,int r,int x,int L,int R,int curd){
    if(L<=l&&r<=R){ sure[x]=lef[x],use[++len]=x;return;}
    int mid=(l+r)>>1;
    if(mid>=L) dfs1(l,mid,x<<1,L,R,curd+1);
    if(mid<R) dfs1(mid+1,r,x<<1^1,L,R,curd+1);
    if(rd[curd][x-(1<<curd)]=='0'){
        if(sure[x<<1]==-1) sure[x]=-1;
        else if(a[sure[x<<1]]>=dep-curd) sure[x]=sure[x<<1];
        else sure[x]=sure[x<<1^1];
    }
    else{
        if(sure[x<<1^1]==-1) sure[x]=-1;
        else if(a[sure[x<<1^1]]>=dep-curd) sure[x]=sure[x<<1^1];
        else sure[x]=sure[x<<1];
    }
    if(sure[x]!=-1) use[++len]=x;
}
LL res;
void dfs2(int l,int r,int x,int L,int R,int curd,int must){
    if(sure[x]!=-1){
        if(a[sure[x]]>=must) res+=sure[x]+1;
        return;
    }
    if(r<L||l>R){ res+=sum(l,r);return;}
    int mid=(l+r)>>1,t=rd[curd][x-(1<<curd)]-'0';
    dfs2(l,mid,x<<1,L,R,curd+1,max(must,(!t)?dep-curd:0));
    dfs2(mid+1,r,x<<1^1,L,R,curd+1,max(must,t?dep-curd:0));
}
int str[N],stx[N],stde[N];
void work(){
    F(i,0,3) cin>>X[i];
    F(i,0,n-1) a[i]=ori[i]^X[(i+1)%4];
    build(0,(1<<dep)-1,1,0);
    LL ans=0;
    F(i,1,m){
        res=len=0;
        dfs1(0,str[i],stx[i],0,c[i],stde[i]);
        dfs2(0,str[i],stx[i],0,c[i],stde[i],0);
        F(j,1,len) sure[use[j]]=-1;
        ans^=i*res;
    }
    cout<<ans<<'\n';
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    cin>>n>>m;
    while((1<<dep)<n) dep++;
    F(i,0,n-1) cin>>ori[i];
    F(i,1,m){
        cin>>c[i];c[i]--;
        int r=(1<<dep)-1,x=1,d=0;
        while(r&&r/2>=c[i]) r/=2,x<<=1,d++;
        str[i]=r,stx[i]=x,stde[i]=d;
    }
    DF(i,dep-1,0) cin>>rd[i];
    int T;cin>>T;
    while(T--) work();
    return 0;
}
